$(function () {
    $("#jqGrid").jqGrid({
        url: 'sys/generator/list',
        datatype: "json",
        colModel: [{label: '表名', name: 'engine', width: 100, key: true, formatter: editLink}, {
            label: '备注', name: 'tablename', width: 100
        }, {label: '最后一个操作时间', name: 'createtime', width: 70}, {label: '类型(表/视图)', name: 'tabletype', width: 40}],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50, 100, 200],
        rownumbers: true,
        rownumWidth: 50,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list", page: "page.currPage", total: "page.totalPage", records: "page.totalCount"
        },
        prmNames: {
            page: "page", rows: "limit", order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

function editLink(cellValue, options, rowdata, action) {
    return "<a href='#column.html?table=" + cellValue + "' onClick='openDict(" + cellValue + ")'>" + cellValue + "</a>";
}

function openDict(cellValue) {
    var table = {cellValue};
    var urlPram = cellValue.baseURI.split("?")[1];
    if (null == urlPram) return;
    window.open('column.html?' + urlPram, 'newwindow',
        'height=500, width=1100, top=170, left=300, toolbar=no, '
        + 'menubar=no, scrollbars=no, resizable=no,location=n o, status=no')
}

var vm = new Vue({
    el: '#rrapp', data: {
        q: {tableName: null}
    }, methods: {
        query: function () {
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'tableName': vm.q.tableName}, page: 1
            }).trigger("reloadGrid");
        }, generator: function () {
            var tableNames = getSelectedRows();
            if (tableNames == null) {
                return;
            }
            location.href = "sys/generator/code?tables=" + tableNames.join();
        }, generator2: function () {
            var tableNames = vm.q.tableName;
            if (tableNames == null) {
                alert("表名不能为空！");
                return;
            }
            location.href = "sys/generator/code?tables=" + tableNames;
        },
        onClickButton: function () {
            var gsr = $("#jqGrid").jqGrid('getGridParam', 'selrow'); //获取选中行的数据
            if (gsr) {
                var subHtml;
                var span='<span class="ui-jqgrid-resize ui-jqgrid-resize-ltr" style="cursor: col-resize;">&nbsp;</span>';
                var div='<div class="ui-th-div ui-jqgrid-sortable">';
                var res = $.ajax({
                    url: 'sys/generator/dictionary?',//table=' + gsr
                    type: "GET",
                    dataType: "json",
                    async: false,
                    data: {
                        table: gsr,
                    },
                    success: function (result) {
                        subHtml = '<table id="tablebc">' +
                            '<thead>' +
                            '<tr>' +
                            '<th class="thxh">'+span+' </th>' +
                            '<th class="thbc1">'+span+'列名</th>' +                   
                            '<th class="thbc1">'+span+'备注</th>' +
                            '<th class="thbc2">'+span+'类型</th>' +
                            '<th class="thbc">'+span+'数据长度</th>' +
                            '<th class="thbc">'+span+'数据精度</th>' +
                            '<th class="thbc">'+span+'是否可空</th>' +
                            '<th class="thbc">'+span+'是否主键</th>' +
                            '</tr>' +
                            '</thead>' +
                            '<tbody>';
                        for (var i = 0; i < result.length; i++) {
                            var data = result[i] ;
                            subHtml += ('<tr class="jqgrow ui-row-ltr" >' +
                                '<td class="tdbc" id="index" style="text-align:center ">' + (i+1) + '</td>' +
                                '<td class="tdbc" id="col_code" >' + data.col_code + '</td>' +
                                '<td class="tdbc" id="col_name"  ">' + data.col_name + '</td>' +
                                '<td class="tdbc" id="col_type"  ">' + data.col_type + '</td>' +
                                '<td class="tdbc" id="data_length"  ">' + data.data_length + '</td>' +
                                '<td class="tdbc" id="data_precision"  ">' + data.data_precision + '</td>' +
                                '<td class="tdbc" id="nullable"  ">' + data.nullable + '</td>' +
                                '<td class="tdbc" id="pk"  ">' + data.pk + '</td>' +
                                '</tr>');
                        }
                        subHtml += '</tbody> </table>';
                    }
                });
                layer.open({
                    type: 1,//页面层
                    title: ['字典详情', 'font-size:20px;background-color:pink;color:#fff'],
                    area: ['1000px', '450px', 'background-color:lightblue'],
                    fixed: false, //不固定
                    maxmin: true,//页面最大化和最小化
                    resize: false,
                    shadeClose: true,
                    anim: 6,//抖动
                    content: subHtml

                });
            } else {
                layer.alert("请选择需要操作的数据行!");
            }
        }
    }
});

